package com.atguigu.yygh.hosp.testmongo;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

@Api(tags = "MongoRepository操作")
@RestController
@RequestMapping("/mongoRepository")
public class MongoRepositoryController {
    @Autowired
    private UserRepository mongoRepository;


    //模糊查询
    @GetMapping("method")
    public void method(){
      //模糊查询
        List<User> list = mongoRepository.findByNameLike("咪");
        System.out.println(list);
    }
    //根据名字和年龄查
    @GetMapping("method2")
    public void method2(){
       List<User> list = mongoRepository.findByNameAndAge("杨咪咪",45);
        System.out.println("list = " + list);
    }

    //添加
    @GetMapping("/create")
    public void create(){
        User user = new User();
        user.setName("杨幂");
        user.setEmail("11211");
        user.setAge(28);
         mongoRepository.save(user);
        System.out.println("user = " + user);
    }
    //修改
    @GetMapping("/update")
    public void  update(){
        //根据id查询
        User user = (User) mongoRepository.findById("612f5fea1e1d8d4c7e7d88c5").get();
        //修改值
        user.setName("杨咪咪");
        user.setAge(45);
        user.setEmail("185");
        mongoRepository.save(user);
        System.out.println("user = " + user);
    }
    //查询所有
    @GetMapping("findAll")
    public void findAll(){
        List<User> list = mongoRepository.findAll();
        System.out.println("list = " + list);
    }
    //条件查询
    @GetMapping("findQuery")
    public void findUserList(){
        User user = new User();
        user.setName("杨咪咪");
        user.setAge(45);
        Example<User> example =Example.of(user);
        List<User> list = mongoRepository.findAll(example);
        System.out.println("list = " + list);
    }
    //模糊查询
    @GetMapping("findLike")
    public void findLike(){
        //设置匹配规则：模糊查询
        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching()//构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变字符串默认匹配规则
                .withIgnoreCase(true);//忽视大小写
        User user = new User();
        user.setName("咪");
        Example<User> example =Example.of(user,matcher);
       List<User> list = mongoRepository.findAll(example);
        System.out.println("list = " + list);
    }


    //分页查询
    @GetMapping("findPage")
    public void findPage(){
        User user = new User();
        user.setName("咪");
        //排序规则sort
        Sort sort =Sort.by(Sort.Direction.DESC,"age");//根据年龄字段降序
        //分页对象pageable  0表示第一页
        Pageable pageable = PageRequest.of(0,1,sort);
        //匹配规则example
        ExampleMatcher matcher =ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<User> example =Example.of(user,matcher);
        Page<User> page = mongoRepository.findAll(example, pageable);
        System.out.println("page = " + page);
    }

    //删除
    @GetMapping("delete")
    public void delete(){
        mongoRepository.deleteById("612f5fea1e1d8d4c7e7d88c5");
    }
}
